// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace ValidationAspects.Factories
{
	public class LengthInRange : ValidatorFactoryBase<string>
	{
		public int Minimum { get; set; }
		public int Maximum { get; set; }

		#region Overrides of ValidatorFactoryBase<string>
		protected override Action<string, IValidationContext> ValidateAction
		{
			get { return (v, c) => { if (v != null && (v.Length < Minimum || v.Length > Maximum)) throw new ValidateLengthInRangeException(Minimum, Maximum, v.Length); }; }
		}

		public override IValidator<T> Create<T>()
		{
			if (Minimum >= Maximum)
				throw new ArgumentOutOfRangeException("Minimum", string.Format("Range minimum must be less than maximum [value:{0}].", Maximum));

			return base.Create<T>();
		}
		#endregion
	}
}